package com.bynk.bynk_demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@RestController
public class FileController {


    @PostMapping("upload")
    public String upload(String name, MultipartFile file, HttpServletRequest request) throws IOException {

        String information = "File{" +
                "文件大小=" + file.getSize() +
                ",文件名='" + file.getName() +
                ", 文件原始名='" + file.getOriginalFilename() +
                ", 文件类型='" + file.getContentType() +
                //request请求信息,获取上下文路径,即服务器路径
                ", path='" + request.getServletContext().getRealPath("/") +
                '}';

//        判断路径是否存在
        String path = "D:\\workspace\\dev_java\\bynk_demo\\src\\main\\resources\\static";
//        if (new File(path).exists()) {
//
//        }


        //transferTo将数据传输到文件流中保存
        file.transferTo(new File(path+"\\"+file.getOriginalFilename()));

        return information;
    }
}
